﻿#region

using System.Collections.Generic;
using System.Text.RegularExpressions;

#endregion


namespace OMS.Ice.T4Generator.Syntax
{
    internal abstract class Directive : Part
    {
        protected Directive( Match match, MacroResolver macroResolver )
        {
            Match = match;

            Attributes = new Dictionary<string, string>();

            var keys = Match.Groups["key"].Captures;
            var values = Match.Groups["value"].Captures;

            if( keys.Count != values.Count )
                throw new T4Exception( "Error in T4 directives: Different number of keys and values." );

            for( var i = 0; i < keys.Count; i++ )
                Attributes.Add( keys[i].Value.ToLower(), macroResolver.Resolve( values[i].Value ));
        }

        protected Match Match { get; private set; }

        protected MacroResolver MacroResolver { get; private set; }

        public override int Index
        {
            get { return Match.Index; }
        }


        public override int Length
        {
            get { return Match.Length; }
        }

        public override int Position
        {
            // Index and Position are the same for all directives.
            get { return Match.Index; }
        }


        public IDictionary<string, string> Attributes { get; private set; }
    }
}